import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

import com.dinim.graphics.GraphicsContextSwingImpl;
import com.dinim.graphics.IGraphicsContext;
import com.dinim.matrix.twodimensional.IPoint2D;
import com.dinim.object.Line2D;
import com.dinim.object.Object2D;
import com.dinim.object.Point2D;


public class ClosestPointTester {

    final static IPoint2D point = new Point2D(125, 70);
    static IGraphicsContext gc;

    public static void main(String[] args) {

        JFrame f = new JFrame();
        f.setSize(300, 300);
        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
        final Canvas canvas = new Canvas();
        f.add(canvas);
        f.setVisible(true);
        canvas.addKeyListener(new KeyListener(){
            public void keyPressed(KeyEvent e) {}
            public void keyReleased(KeyEvent e) {}
            public void keyTyped(KeyEvent e) {}
        });
        canvas.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e) {
                point.setX(e.getX());
                point.setY(e.getY());
            }
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
            public void mousePressed(MouseEvent e) {}
            public void mouseReleased(MouseEvent e) {}
        });
        canvas.addMouseMotionListener(new MouseMotionListener(){
            public void mouseDragged(MouseEvent e) {
                point.setX(e.getX());
                point.setY(e.getY());
            }
            public void mouseMoved(MouseEvent e) {}
        });

        Point2D p1 = new Point2D(50, 10);
        Point2D p2 = new Point2D(30, 100);
        Point2D p3 = new Point2D(100, 100);
        Point2D p4 = new Point2D(120, 80);
        Point2D p5 = new Point2D(100, 50);
        IPoint2D[] points = new Point2D[]{p1, p2, p3, p4, p5};
        int[] connections = new int[]{0, 1, 2, 3, 4};
        //Object2D poly1 = new Polygon2D(points, connections);
//        Object2D poly1 = new Rectangle2D(10,10,100,100);
//        Object2D poly1 = new Star2D(10,10,100,100, 4);
        Object2D poly1 = new Line2D(10,300,300,300);
        poly1.rotate(2, poly1.getCenterPoint());
//        poly1.translate(90, 30);
        

        
        canvas.createBufferStrategy(2);
        BufferStrategy doublebuffer = canvas.getBufferStrategy();

        int w = 2;
        
        while(true){
            Graphics g = doublebuffer.getDrawGraphics();
            ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            poly1.rotate(0.1, poly1.getCenterPoint());
            
            gc = new GraphicsContextSwingImpl(g);
            gc.setColor(Color.white);
            gc.drawFilledRectangle(0, 0, canvas.getWidth(), canvas.getHeight());
            gc.setColor(Color.black);

            Line2D l = (Line2D) poly1;
            Point pe = PointLineDistance.ptClosestTo((int)Math.round(l.getX1()), (int)Math.round(l.getY1()), (int)Math.round(l.getX2()), (int)Math.round(l.getY2()), new Point((int)Math.round(point.getX()), (int)Math.round(point.getY())));
            gc.drawOval(pe.x-w, pe.y-w, pe.x+w, pe.y+w);
            
            gc.drawFilledRectangle((int)point.getX()-w, (int)point.getY()-w, (int)point.getX()+w, (int)point.getY()+w);
            poly1.draw(gc);
            
            doublebuffer.show();
            
            
            
            try {
                Thread.sleep(10);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }
    }
}
